package four;

import com.fasterxml.jackson.databind.ObjectMapper;
import four.model.IgnoreBean;
import four.model.MyBean;

import java.io.IOException;
import java.net.URL;

public class AnnotationConfigurationExample {
    public static void main(String[] args) throws IOException {
        diffFieldUseAnnotation();
        ignorePropertiesUseAnnotation();
    }

    private static void ignorePropertiesUseAnnotation() throws IOException {
        URL resource = AnnotationConfigurationExample.class.getClassLoader().getResource("four/ignoreBean.json");
        ObjectMapper mapper = new ObjectMapper();
        IgnoreBean ignoreBean = mapper.readValue(resource, IgnoreBean.class);
        //MyBean{_name='wpp', _age=10, _address='SHANGHAI'}
        System.out.println(ignoreBean);
    }

    private static void diffFieldUseAnnotation() throws IOException {
        URL resource = AnnotationConfigurationExample.class.getClassLoader().getResource("four/mybean.json");
        ObjectMapper mapper = new ObjectMapper();
        MyBean myBean = mapper.readValue(resource, MyBean.class);

        //IgnoreBean{id='null', remark='null', date='null', name='wpp', age=10, address='SHANGHAI'}
        System.out.println(myBean);


        //对象到josn

        String s = mapper.writeValueAsString(myBean);

        //{"name":"wpp","age":10,"address":"SHANGHAI"}
        System.out.println(s);

    }
}
